package com.huluobo.a2308douyindemo.videos

import androidx.lifecycle.MutableLiveData
import com.huluobo.a2308douyindemo.bean.CollectBean
import com.huluobo.a2308douyindemo.bean.CommentBean
import com.huluobo.a2308douyindemo.bean.LikeBean
import com.huluobo.a2308douyindemo.bean.LikeVideoBean
import com.huluobo.a2308douyindemo.bean.ReplyBean
import com.huluobo.a2308douyindemo.bean.VideoBean
import com.huluobo.a2308douyindemo.db.DouYinDatabase
import com.huluobo.a2308douyindemo.mvvm.BaseViewModel
import com.huluobo.a2308douyindemo.net.ApiService
import com.huluobo.a2308douyindemo.net.Res
import com.huluobo.a2308douyindemo.utils.UnPeekLiveData
import dagger.hilt.android.lifecycle.HiltViewModel
import okhttp3.RequestBody
import javax.inject.Inject

/**
 *  Created by LC on 2025/10/22.
 */
@HiltViewModel
class VideoViewModel @Inject constructor(private val apiService: ApiService, private val db: DouYinDatabase) :
    BaseViewModel() {
    val getVideosSuccess by lazy { MutableLiveData<Res<List<VideoBean>>>() }
    fun getVideos(page: Int, pageSize: Int) {
        executeNetwork(
            { apiService.getVideos(page, pageSize) },
            getVideosSuccess::postValue
        )
    }

    val likeVideoSuccess by lazy { MutableLiveData<Res<LikeBean>>() }
    fun likeVideo(map: Map<String, Any>) {
        executeNetwork(
            { apiService.likeVideo(mapToJson(map)) },
            likeVideoSuccess::postValue
        )
    }

    val getLikeVideosSuccess by lazy { MutableLiveData<Res<List<VideoBean>>>() }
    fun getLikeVideos(sourceFlag: String, userId: Int) {
        executeNetwork(
            { apiService.getLikeVideos(sourceFlag, userId) },
            getLikeVideosSuccess::postValue
        )
    }

    val unLikeVIdeoSuccess by lazy { MutableLiveData<Res<String>>() }
    fun unLikeVideo(map: Map<String, Any>) {
        executeNetwork(
            { apiService.unLikeVideo(mapToJson(map)) },
            unLikeVIdeoSuccess::postValue
        )
    }

    val getCommentSuccess by lazy { MutableLiveData<Res<List<CommentBean>>>() }
    fun getComments(dateType: Int, itemId: String) {
        executeNetwork(
            { apiService.getComments(dateType, itemId) },
            getCommentSuccess::postValue
        )
    }

    val sendCommentSuccess by lazy { MutableLiveData<Res<CommentBean>>() }
    fun sendComment(commentBean: CommentBean) {
        executeNetwork(
            { apiService.sendComment(objToJson(commentBean)) },
            sendCommentSuccess::postValue
        )
    }

    val replyCommentSuccess by lazy { MutableLiveData<Res<ReplyBean>>() }
    fun replyComment(replyBean: ReplyBean) {
        executeNetwork(
            { apiService.replyComment(objToJson(replyBean)) },
            replyCommentSuccess::postValue
        )
    }

    val commentLikeSuccess by lazy { MutableLiveData<Res<Boolean>>() }
    fun commentLike(commentId: Int) {
        executeNetwork(
            { apiService.commentLike(commentId) },
            commentLikeSuccess::postValue
        )
    }

    val focusedSuccess by lazy { MutableLiveData<Res<Boolean>>() }
    fun focusedUser(userId: Int, authId: String) {
        executeNetwork(
            { apiService.focusUser(userId, authId) },
            focusedSuccess::postValue
        )
    }

    val unFocusSuccess by lazy { MutableLiveData<Res<Boolean>>() }
    fun unFocusUser(userId: Int, authId: String) {
        executeNetwork(
            {
                "取消关注"
                apiService.unFocsUser(userId, authId)
            },
            unFocusSuccess::postValue
        )
    }

    val collectVideoSuccess by lazy { MutableLiveData<String>() }
    fun collectVideo(videoBean: VideoBean) {
        executeDb(
            {
                //当前大括号中是个方法,最后一句需要是当前方法的返回值
                //如果方法中只有一句,就直接作为返回值使用
                // return@executeDb 可以省略
                db.getVideoDao().insertVideoInfo(videoBean)
                "收藏成功"
            },
            collectVideoSuccess::postValue
        )
    }

    val collectVideoNetSuccess by lazy { MutableLiveData<Res<CollectBean>>() }
    fun collectVideoNet(collectBean: CollectBean) {
        executeNetwork(
            { apiService.collectVideo(objToJson(collectBean)) },
            collectVideoNetSuccess::postValue
        )
    }

    val likeVideoDbSuccess by lazy { UnPeekLiveData<String>() }
    fun likeVideoDb(likeVideoBean: LikeVideoBean) {
        executeDb(
            {
                db.getLikeDao().insertLikeVideo(likeVideoBean)
                "点赞成功"
            },
            likeVideoDbSuccess::setContentValue
        )
    }

    val deleteLikeVideoSuccess by lazy { UnPeekLiveData<String>() }
    fun unLikeVideoDb(likeVideoBean: LikeVideoBean) {
        executeDb(
            {
                db.getLikeDao().deleteLikeVideo(likeVideoBean)
                "取消点赞成功"
            },
            deleteLikeVideoSuccess::setContentValue
        )
    }

    val queryLikeVideoSuccess by lazy { UnPeekLiveData<LikeVideoBean?>() }
    fun queryLikeVideo(id: Int) {
        executeDb(
            { db.getLikeDao().queryLikeVideoById(id) },
            queryLikeVideoSuccess::setContentValue
        )
    }

    val queryAllCollectSuccess by lazy { UnPeekLiveData<Res<List<VideoBean>>>() }
    fun queryAllCollect(userId: Int, sourceFlag: String) {
        executeNetwork(
            { apiService.getCollect(userId, sourceFlag) },
            queryAllCollectSuccess::setContentValue
        )
    }

}